#include "stdafx.h"
#include "resource.h"
#include "ChatManager.h"
#include "ManagerForm.h"

ChatManager* ChatManager::m_this=NULL;
ChatManager::ChatManager(void)
{
}											  

static inline int FindPosition(char **commands,int commands_len,const char *what)
{
	int pos=-1,start=0;
	for(;start<commands_len;++start)
	{
		if(strcmp(commands[start],what)==0)
		{
		   pos=start;
		   break;
		}
	}
	return pos;
}

void ChatManager::DoResultProcess(Json::Value *value)
{
	DWORD message=-1;
	const char *command=(*value)["c"].asCString();
	const char *m=(*value)["m"].asCString();
	int pos=FindPosition(g_command,g_command_len,command);
	CString cs;
	if(pos!=-1&&pos<plast&&strcmp(g_command_ok[pos],m)==0)
	{
		switch(pos)
		{
		case plogin:
		break;
		case pheart:
		break;
		case plogout:
		break;
		case pgetuserinfo:
			message=WM_GET_USER_INFO;
			//SendMessage(CManagerForm::Instance()->m_hWnd,WM_GET_USER_INFO,(WPARAM)nullptr,(LPARAM)value);
		break;
		case pgetusersinfo:
		break;
		case pgetallmapusers:
			message=WM_MAPS_USERS;
			//SendMessage(CManagerForm::Instance()->m_hWnd,WM_MAPS_USERS,(WPARAM)nullptr,(LPARAM)value);
		break;
		case pchangemap:
		break;
		case psystem:
		break;
		case pgetusers:
		break;
		case pgetonlinecount:
		break;
		case pgetmapusers:
		break;
		case pchatmap:
		break;
		case pchatworld:
		break;
		case pchatfriend:
		break;
		case pchatprivate:
		break;
		case pchatoffline:
		break;
		case pcreateteam:
		break;
		case pdestroyteam:
		break;
		case pteamkick:
		break;
		case pjointeam:
		break;
		case pquitteam:
		break;
		case pchatteam:
		break;
		case pchatleague:
		break;
		case pfriendstatus:
		break;
		case paddblacklist:
			message=WM_ADD_BLACK_LIST_OK;
		break;
		case premoveblacklist:
			message=WM_REMOVE_BLACK_LIST_OK;
		break;
		case pkickuser:
			message=WM_KICK_USER_OK;
		break;
		case pdeny:
			cs.Format(L"%S",value->toStyledString().c_str());
			AfxMessageBox(cs);
			break;
		default:
			break;
		}
		(message!=-1)&&SendMessage(CManagerForm::Instance()->m_hWnd,message,(WPARAM)nullptr,(LPARAM)value);
	}else
	{
			cs.Format(L"%S",value->toStyledString().c_str());
			AfxMessageBox(cs);
	}

}
ChatManager* ChatManager::Instance(ChatClient* client)
{
	if(m_this==nullptr)
	{
		m_this=new ChatManager(client);
	}
	return m_this;
}
ChatManager::ChatManager(ChatClient* client)
{
	m_client=client;
}

ChatManager::~ChatManager(void)
{
	delete m_this;
}
